/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

/// <reference path="../base.js" />
/// <reference path="./sector.js" />
/// <reference path="../data/constants.js" />

/* Utility scripts for Pardus Sector Tool
 * (C) Yassen 2010
 *
 * object.js
 * Defines functions for getting/setting object information.
 *
 * Functions:
 * array   GetList(object)
 * void    SetList(object, list)
 * int     GetIndex(object, id)
 * void    Add(object, data)
 * void    Update(object, data)
 * void    Remove(object, id)
 * bool    Exists(object, id)
 * Object  Get(object, id)
 *
 */

/// Returns all objects of a certain type (Space Farm, Nebula Plant, etc)
Utility.Object.GetByType = function(object)
{
  var objects = Utility.Object.GetList(object.ClassName);
  var objectsOfType = [];

  for (var i = 0; i < objects.length; i++)
  {
    var tObject = objects[i];

    if (tObject.Info() == object.Info())
      objectsOfType.push(tObject);
  }

  return objectsOfType;
}

Utility.Object.GetList = function(object)
{
  /// <summary>Gets the sector's list of objects.</summary>
  /// <param name="object" type="eObject">Object to get the list for.</param>
  /// <returns type="Array">The List</returns>

  if (object && object.ClassName)
    object = object.ClassName;

  switch (object)
  {
    case eBuilding: return Global.DisplayedSector.Buildings;
    case ePlanet: return Global.DisplayedSector.Planets;
    case eStarbase: return Global.DisplayedSector.Starbases;
    case eBackground: return Global.DisplayedSector.Backgrounds;
    default: return null;
  }
}

Utility.Object.SetList = function(object, list)
{
  if (object && object.ClassName)
    object = object.ClassName;

  switch (object)
  {
    case eBuilding: Global.DisplayedSector.Buildings = list; break;
    case ePlanet: Global.DisplayedSector.Planets = list; break;
    case eStarbase: Global.DisplayedSector.Starbases = list; break;
    case eBackground: Global.DisplayedSector.Backgrounds = list; break;
    default: return false;
  }
}

Utility.Object.GetIndex = function(object, id)
{
  var objects = Utility.Object.GetList(object);

  for(var i = 0; i < objects.length; i++)
    if(objects[i].ID == id)
      return i;

  return null;
}

Utility.Object.Add = function(data)
{
  var object = data.ClassName;
  
  var objects = Utility.Object.GetList(object);
  objects.push(data);
  Utility.Object.SetList(object, objects);
}

Utility.Object.Update = function(data)
{
  var object = data.ClassName;
  var objects = Utility.Object.GetList(object);
  objects[Utility.Object.GetIndex(object, data.ID)] = data;
  Utility.Object.SetList(object, objects);
}

Utility.Object.Delete = function(id, object)
{
  if (object == undefined)
    object = Utility.Object.GetType(id);

  var objects = Utility.Object.GetList(object);
  objects.splice(Utility.Object.GetIndex(object, id), 1);
  Utility.Object.SetList(object, objects);
}

Utility.Object.Exists = function(id, object)
{
  if (object == undefined)
    return Utility.Object.GetType(id) != eNull;

  else
    return Utility.Object.GetIndex(object, id) != undefined;
}

Utility.Object.Get = function(id, object)
{
  if (object == undefined)
    object = Utility.Object.GetType(id);
    
  return Utility.Object.GetList(object)[Utility.Object.GetIndex(object, id)];
}

Utility.Object.GetType = function(id)
{
  if (Utility.Object.GetIndex(eBuilding, id) != undefined)
    return eBuilding;

  else if (Utility.Object.GetIndex(ePlanet, id) != undefined)
    return ePlanet;

  else if (Utility.Object.GetIndex(eStarbase, id) != undefined)
    return eStarbase;

  else if (Utility.Object.GetIndex(eBackground, id) != undefined)
    return eBackground;

  else
    return eNull;
}

Utility.Object.GetSelected = function(type)
{
  var list = Utility.Object.GetList(type);
  var result = [];

  for (var i = 0; i < list.length; i++)
  {
    if (Utility.Tile.IsSelected(list[i].ID))
      result.push(list[i]);
  }
}

Utility.Object.GetAllObjects = function()
{
  return $merge(Utility.Object.GetList(eBuilding), Utility.Object.GetList(ePlanet), Utility.Object.GetList(eStarbase));
}

Utility.Object.GetAllSelectedObjects = function()
{
  return $merge(Utility.Object.GetSelected(eBuilding), Utility.Object.GetSelected(ePlanet), Utility.Object.GetSelected(eStarbase));
}